

local fruit = {'香蕉','苹果','核桃','榴莲'}
--鱼几率
local fishs = {
    ['超级大鱼'] = {1,2},
    ['深海巨鲸'] = {1,2},
    ['泰坦鱼'] = {2,3},
}


ac.game:event '单位-使用物品'(function(_,unit,item)
    local player = unit.owner
    local hero = player.hero or unit
    local name = item.name
    local slotid = item.slotid
    --获得金币
    local golds = item['获得金币']
    if golds then
        local min,max = golds:match('(%d*)-(%d*)')
        local gold = math.random(min,max)
        unit:add_effect([[Abilities\Spells\Items\ResourceItems\ResourceEffectTarget.mdl]]):remove()
        player:add('金币',gold)
        player:send_msg(('获得|cff00ffff%s|r金币'):format(gold))
        local count = 4
        if gold>500 then
            count=5
        elseif gold>1000 then
            count = 6
        elseif gold>2000 then
            count = 20
        end
        for a=1,count do
            player:notify('动画-获得资源',player,'金币',slotid)
        end
    end

    local lumber = item['获得木材']
    if lumber then
        unit:add_effect([[Abilities\Spells\Items\ResourceItems\ResourceEffectTarget.mdl]]):remove()
        player:add('木材',lumber)
        player:send_msg(('获得|cff00ff00%s|r木材'):format(lumber))
        local count = 4
        if lumber>15 then
            count = 5
        elseif lumber>50 then
            count = 7
        elseif lumber>100 then
            count = 10
        end
        for a=1,count do
            player:notify('动画-获得资源',player,'木材',slotid)
        end
    end
    
    if item['几率'] then
        local odds = fishs[name]
        if odds and math.random(100)<=item['几率'] then
            local num = math.random(odds[1],odds[2])
            for a=1,num do
                local name = table.random(fruit)
                local item = hero:add_item(name)
                player:send_msg(('获得|cff00ff00%s|r'):format(item:get_title()))
            end
        end
        local item_str = item['道具']
        --获得道具
        if item_str and math.random(100)<=item['几率'] then
            local items = split_string(item_str,',')
            local name = table.random(items)
            local new = hero:add_item(name)
            player:send_msg(('获得|cff00ff00%s|r'):format(new:get_title()))
        end
        
    end
    

        
    if name=='麦辣鸡块' then
        local cards = hero:get_data('展示天赋表') or {}
        local list = {}
        for a=1,12 do
            table.insert(list,cards[a])
        end
        if #list>0 then
            local temp = table.random(list)
            hide_card(temp)
        end

    elseif name=='黑暗焚香' then
        local enemy = ac.enemy
        if enemy then
            local buff = hero:find_buff '黑暗焚香'
            if not buff then
                buff = hero:add_buff('黑暗焚香'){art = item.art,tip = [[怪物加速中]]}
            end
            buff.stack = (buff.stack or 0) + 1
            enemy.speed = enemy.speed + 0.5
            ac.wait(60*1000,function()
                enemy.speed = enemy.speed - 0.5
                buff.stack = (buff.stack or 0) - 1
                if buff.stack==0 then
                    buff:remove()
                end
            end)
        end
    elseif name=='香蕉' then
        local num = math.random(3)
        hero:add_level(num)
        player:send_msg(('获得“|cff00ff00英雄等级+%0.f|r”'):format(num))


    elseif name=='苹果' then
        local num = 20
        local add = 1
        hero:add('基础敏捷',num)
        hero:add('敏捷加成',add)
        player:send_msg(('获得“|cff00ff00基础敏捷+%s,敏捷加成+%s%%|r”'):format(num,add))


    elseif name=='核桃' then
        local num = 20
        local add = 1
        hero:add('基础智力',num)
        hero:add('智力加成',add)
        player:send_msg(('获得“|cff00ff00基础智力+%s|r,智力加成+%s%%”'):format(num,add)) 

    elseif name=='榴莲' then
        local num = 20
        local add = 1
        hero:add('基础力量',num)
        hero:add('力量加成',add)
        player:send_msg(('获得“|cff00ff00基础力量+%s,力量加成+%s%%|r”'):format(num,add))

    elseif name=='骰子' or name=='金骰子' then
        hero:add('抽卡次数',item.data1)
        player:send_msg(('获得“|cff00ff00抽卡次数+%0.f|r”'):format(item.data1))

    elseif name=='刷新券' or name=='超级刷新券' then
        hero:add('刷新次数',item.data1)
        player:send_msg(('获得“|cff00ff00刷新次数+%0.f|r”'):format(item.data1))

    elseif name=='金币堆' or name=='财宝堆' then
        local gold = tonumber(item.data1)
        player:add('金币',gold)
        local count = 4
        if gold>500 then
            count=5
        elseif gold>1000 then
            count = 6
        elseif gold>2000 then
            count = 8
        end
        for a=1,count do
            player:notify('动画-获得资源',player,'金币',slotid)
        end
        player:send_msg(('获得|cff00ffff%s|r金币'):format(item.data1))

    elseif name=='木材堆' or name=='一大捆木材' then
        local lumber = tonumber(item.data1)
        local point = hero:get_point()
        player:add('木材',lumber)
        local count = 4
        if lumber>15 then
            count=5
        elseif lumber>50 then
            count = 7
        elseif lumber>100 then
            count = 10
        end
        for a=1,count do
            player:notify('动画-获得资源',player,'木材',slotid)
        end
        player:send_msg(('获得|cff00ff00%s|r木材'):format(item.data1))
        
    elseif name=='金神像' then
        hero:add('每秒金币',item.data1)
    elseif name=='鱼神眷顾' then
        hero:add('每秒木材',item.data1)

    elseif name=='蛋炒饭' then
        hero:add_buff('蛋炒饭')
        {
            art = item.art,
            tip = [[经验获取提升]],
            time = 120,
            on_add = function(buff)
                buff:gc(hero:add('经验加成',item.data1))
            end
        }
    elseif name=='生命果实' then
        hero:add('复活次数',1)
        
    elseif name=='天赋宝箱' then
        local card = item.card
        if card then
            if player:get_data(card.name) and hero:find_skill(card.name)==nil then
                local info = {card}
                player:set_data('当前额外卡组',info)
                player:notify('卡池-显示抽卡',player,info)
            else
                player:add('木材',200)
                player:send_msg('|cff00ff00已有天赋或者不是你当前所属系列的天赋，获得200木材|r”')
            end
        end

    elseif name=='彩票'  then
        local a = math.random(1,20)/10
        player:set('木材',player:get('木材') * a)
        if a>1 then
            player:send_msg(('|cff00ff00开奖！倍率：%s|r”'):format(a))
        else
            player:send_msg(('|cffff0000开奖！倍率：%s|r”'):format(a))
        end
        
    elseif name=='黑商补给'  then
        player:notify('玩家-添加炮弹',player,item.data1)
    end

end)


ac.game:event '单位-获得神符'(function(_,unit,item)
    if item.name=='彩票' then
        local player = unit.owner
        local a = math.random(10,200)/100
        player:set('木材',player:get('木材') * a)
        player:send_msg(('|cff00ff00开奖！倍率：%s|r”'):format(a))
    end
end)

ac.game:event '单位-获得物品'(function(_,hero,item)
    if item.name=='龙蛋' then
        local player = hero.owner
        item:gc(hero:event '单位-击杀'(function()
            item:add_charges(-1)
            if item:get_charges()<=0 then
                item:remove()
                local card = get_card_data('远古龙族')
                if hero:find_skill(card.name)==nil then
                    local info = {card}
                    player:set_data('当前额外卡组',info)
                    player:notify('卡池-显示抽卡',player,info)
                else
                    player:add('木材',200)
                    player:send_msg('|cff00ff00已有天赋获得200木材|r”')
                end
            end
        end))
    end
end)
